package com.study.pojo.common;

import com.study.Utils.NowDateTime;
import com.study.enums.ResultCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 统一返回类型
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {
    private String timestamp = NowDateTime.ChinaTime();// 时间
    private Integer code;//状态码
    private String msg;//状态信息
    private T data; //返回对象

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    //返回成功状态码，数据对象
    public static <T> Result<T> success(T data) {
        return new Result<>(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg(), data);
    }

    //默认成功 返回code和msg
    public static <T> Result<T> success() {
        return new Result<>(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg(), null);
    }

    //自定义成功返回消息
    public static <T> Result<T> success(String message, T data) {
        return new Result<>(ResultCodeEnum.SUCCESS.getCode(), message, data);
    }

    //指定成功
    public static <T> Result<T> success(IResult errorResult, T data) {
        return new Result<>(errorResult.getCode(), errorResult.getMsg(), data);
    }

    //指定成功
    public static Result<?> success(IResult successResult) {
        return new Result<>(successResult.getCode(), successResult.getMsg(), null);
    }


    //默认失败 返回code和msg
    public static Result<?> failed() {
        return new Result<>(ResultCodeEnum.FAILED.getCode(), ResultCodeEnum.FAILED.getMsg(), null);
    }

    //自定义失败返回消息
    public static Result<?> failed(String message) {
        return new Result<>(ResultCodeEnum.FAILED.getCode(), message, null);
    }

    //指定错误
    public static Result<?> failed(IResult errorResult) {
        return new Result<>(errorResult.getCode(), errorResult.getMsg(), null);
    }

    //返回自定义错误码和消息
    public static Result<?> failed(IResult errorResult, String message) {
        return new Result<>(errorResult.getCode(), message, null);
    }
}